/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.InternalEList;
import org.omg.deployment.DeploymentPackage;
import org.omg.deployment.DeploymentPlan;
import org.omg.deployment.InstanceDeploymentDescription;
import org.omg.deployment.PlanLocality;
import org.omg.deployment.PlanLocalityKind;
import org.omg.xmi.impl.XMIImpl;

import com.zeligsoft.omg.lwccm.deployment.DeploymentUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Plan Locality</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.omg.deployment.impl.PlanLocalityImpl#getConstraint <em>Constraint</em>}</li>
 *   <li>{@link org.omg.deployment.impl.PlanLocalityImpl#getConstrainedInstance <em>Constrained Instance</em>}</li>
 *   <li>{@link org.omg.deployment.impl.PlanLocalityImpl#getConstrainedInstanceRef <em>Constrained Instance Ref</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class PlanLocalityImpl extends XMIImpl implements PlanLocality {
	/**
	 * The default value of the '{@link #getConstraint() <em>Constraint</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getConstraint()
	 * @generated
	 * @ordered
	 */
	protected static final PlanLocalityKind CONSTRAINT_EDEFAULT = PlanLocalityKind.SAME_PROCESS;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected PlanLocalityImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return DeploymentPackage.eINSTANCE.getPlanLocality();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PlanLocalityKind getConstraint() {
		return (PlanLocalityKind) getGroup().get(
				DeploymentPackage.eINSTANCE.getPlanLocality_Constraint(), true);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setConstraint(PlanLocalityKind newConstraint) {
		((FeatureMap.Internal) getGroup()).set(
				DeploymentPackage.eINSTANCE.getPlanLocality_Constraint(),
				newConstraint);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InstanceDeploymentDescription> getConstrainedInstance() {
		return getGroup().list(
				DeploymentPackage.eINSTANCE
						.getPlanLocality_ConstrainedInstance());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<InstanceDeploymentDescription> getConstrainedInstanceRef() {
		final EList<InstanceDeploymentDescription> constrainedInstances = new BasicEList<InstanceDeploymentDescription>();

		final DeploymentPlan plan = DeploymentUtil.INSTANCE
				.getDeploymentPlan(this);

		if (plan != null) {
			for (InstanceDeploymentDescription next : getConstrainedInstance()) {
				final InstanceDeploymentDescription instance = plan
						.findInstance(next.getIdref());
				if (instance != null) {
					constrainedInstances.add(instance);
				}
			}
		}

		return ECollections.unmodifiableEList(constrainedInstances);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE:
			return ((InternalEList<?>) getConstrainedInstance()).basicRemove(
					otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINT:
			return getConstraint();
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE:
			return getConstrainedInstance();
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE_REF:
			return getConstrainedInstanceRef();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINT:
			setConstraint((PlanLocalityKind) newValue);
			return;
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE:
			getConstrainedInstance().clear();
			getConstrainedInstance()
					.addAll((Collection<? extends InstanceDeploymentDescription>) newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINT:
			setConstraint(CONSTRAINT_EDEFAULT);
			return;
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE:
			getConstrainedInstance().clear();
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINT:
			return getConstraint() != CONSTRAINT_EDEFAULT;
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE:
			return !getConstrainedInstance().isEmpty();
		case DeploymentPackage.PLAN_LOCALITY__CONSTRAINED_INSTANCE_REF:
			return !getConstrainedInstanceRef().isEmpty();
		}
		return super.eIsSet(featureID);
	}

} //PlanLocalityImpl
